﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LevelManager : MonoBehaviour {

	[SerializeField] private GameObject enemyPrefab;
	[SerializeField] private Transform[] spawnTrans;
	[SerializeField] private bool instantiateInPivot;
	[SerializeField] private string nextLevel;
	[SerializeField] private GameObject weaponRewardPrefab;

	private Vector3 pivotOffset = Vector3.zero;

	void Start () 
	{
		foreach(Transform trans in spawnTrans)
        {
            if (instantiateInPivot)
            {
				// 获取要实例化的物体的SpriteRenderer组件
				SpriteRenderer spriteRenderer = enemyPrefab.GetComponentInChildren<SpriteRenderer>();

				// 计算pivot相对于center的偏移量
				pivotOffset = spriteRenderer.bounds.center - spriteRenderer.bounds.min;
			}
			Instantiate(enemyPrefab, trans.position - pivotOffset, trans.rotation);
		}
	}

	bool flag;
	void Update()
    {
        if (flag)
        {
			return;
        }
		if(GameObject.FindGameObjectWithTag("Enemy") == null)
        {
			if(nextLevel != null)
            {
                if ("LevelEnd".Equals(nextLevel))
                {
					GameSceneManager.GetInstance().LoadSceneToLevelEnd();
					flag = true;
					return;
                }
				//解锁下一关卡
				GameSceneManager.GetInstance().levels.Add(nextLevel);
			}
            //获得基本武器
            if (weaponRewardPrefab)
            {
				Instantiate(weaponRewardPrefab, transform.position, transform.rotation);
			}
        }
    }
	
}
